<?php


namespace app\openwx\controller;

use app\common\library\wechat\WxBizMsgCrypt;
use think\Db;
use think\facade\Cache;
use think\facade\Config;
use think\facade\Log;
use think\facade\Request;
use app\common\library\Openssl;


class Wxuser extends Base
{

    public function get_openid()
    {
        if (input('?domain')) {
            // dump(input());
            $id = db::name('product_run')->where('domain|diy_domain', '=', input('domain'))->value('id');
            $info = db::name('product_wx')->where('product_run_id', $id)->find();
            if (input('get.state') == 'panpan') {

                if (input('?get.code')) {
                    //dump(input());
                    $param['appid'] = $info['authorizer_appid'];
                    $param['code'] = input('get.code');
                    $param['grant_type'] = 'authorization_code';
                    $param['component_appid'] = config('param.component_appid');
                    $component_access_token = DB::name('config')->where('id', 112)->cache('10')->value('value');
                    $param['component_access_token'] = $component_access_token;
                    $url = 'https://api.weixin.qq.com/sns/oauth2/component/access_token?' . http_build_query($param);
                    $content = file_get_contents($url);
                    $content = json_decode($content, true);
                    //dump($param);
//                    dump(input());
//                    dump($content);
                    //exit;
                    if (isset($content['openid']) && input('url')) {
                        $this->redirect(input('url') . '?openid=' . $content['openid']);
                    } else {
                        $this->assign('info', '稍后再试，error:' . $content['errcode']);
                        return view('work/error');
                    }
                } else {
                    //用户禁止授权
                    echo 'error';
                }
            };
            $param['appid'] = $info['authorizer_appid'];
            $param['redirect_uri'] = url('/wxuser/get_openid?domain=' . input('domain') . '&url=' . input('url'), '', '', true);
            $param['response_type'] = 'code';
            $param['scope'] = 'snsapi_base';
            $param['state'] = 'panpan';
            $param['component_appid'] = config('param.component_appid');
            $url = 'https://open.weixin.qq.com/connect/oauth2/authorize?' . http_build_query($param) . '#wechat_redirect';
            // echo $url;
            //dump($param);
            //$url = 'https://open.weixin.qq.com/connect/oauth2/authorize?appid=' . $info['appid'] . '&redirect_uri=REDIRECT_URI&response_type=code&scope=SCOPE&state=STATE&component_appid=component_appid#wechat_redirect';
            $this->redirect($url);
        }
    }


}